package com.sea.operate.service.impl;

import com.codecrane.core.Constants;
import com.codecrane.core.power.entity.PowerUser;
import com.codecrane.core.web.BatchImportUtils;
import com.sea.operate.entity.Related;
import com.sea.operate.entity.RelatedSku;
import com.sea.operate.entity.RelatedSkuPrice;
import com.sea.operate.entity.RelatedSkuPriceRecord;
import com.sea.operate.mapper.RelatedSkuMapper;
import com.sea.operate.mapper.RelatedSkuPriceMapper;
import com.sea.operate.service.RelatedService;
import com.sea.operate.service.RelatedSkuPriceRecordService;
import com.sea.operate.service.RelatedSkuPriceService;
import com.sea.operate.service.RelatedSkuService;
import com.sea.product.entity.Channel;
import com.sea.product.entity.Product;
import com.sea.product.service.ChannelService;
import com.sea.product.service.ProductService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.List;


/**
 * @author 陈鸿飞
 */
@Service
public class RelatedSkuPriceServiceImpl implements RelatedSkuPriceService {

    @Autowired
    private RelatedSkuPriceMapper relatedSkuPriceMapper;
    @Autowired
    private RelatedSkuService relatedSkuService;
    @Autowired
    private RelatedSkuMapper relatedSkuMapper;

    @Autowired
    private RelatedSkuPriceRecordService relatedSkuPriceRecordService;

    @Override
    public RelatedSkuPrice findByRelatedIds(Integer relatedId) {
        return relatedSkuPriceMapper.findByRelatedIds(relatedId);
    }

    @Override
    public List<RelatedSkuPrice> findByDate(RelatedSkuPrice relatedSkuPrice) {
        return relatedSkuPriceMapper.findByDate(relatedSkuPrice);
    }

    @Override
    public List<RelatedSkuPrice> findByCnd(RelatedSkuPrice relatedSkuPrice) {
        return relatedSkuPriceMapper.findByCnd(relatedSkuPrice);
    }

    @Override
    public int save(RelatedSkuPrice relatedSkuPrice) {
        return relatedSkuPriceMapper.insert(relatedSkuPrice);
    }

    @Override
    public int updateByRelatedId(RelatedSkuPrice relatedSkuPrice) {
        return relatedSkuPriceMapper.updateByRelatedId(relatedSkuPrice);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importRelatedSkuPrice(InputStream inputStream, String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<RelatedSkuPrice> list = BatchImportUtils.iteratorForSkuPrice(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少保存一条SKU信息！");
            } else {
                for (RelatedSkuPrice relatedSkuPrice : list) {
                    RelatedSku sku = relatedSkuService.findByChannelSku(relatedSkuPrice.getRelatedChannelName(), relatedSkuPrice.getRelatedSku());
                    if (null == sku) {
                        throw new RuntimeException("【" + relatedSkuPrice.getRelatedSku() + "】不存在！");
                    } else {
                        relatedSkuPrice.setRelatedId(sku.getId());
                        relatedSkuPrice.setFlag(0);
                        save(relatedSkuPrice);
                    }

                }

            }
        }
    }

    @Override
    public void importRelatedSalesPrice(InputStream inputStream, String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<RelatedSkuPrice> list = BatchImportUtils.iteratorForSalesPrice(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少保存一条SKU信息！");
            } else {
                for(RelatedSkuPrice relatedSkuPrice:list){
                    RelatedSku sku = relatedSkuService.findByChannelSku(relatedSkuPrice.getRelatedChannelName(), relatedSkuPrice.getRelatedSku());
                    if(null==sku){
                        throw new RuntimeException("【"+relatedSkuPrice.getRelatedSku()+"】不存在！");
                    }else{
                        RelatedSkuPrice price = findByRelatedIds(sku.getId());
                        if(null==price){
                            relatedSkuPrice.setRelatedId(sku.getId());
                            relatedSkuPrice.setFlag(0);
                            save(relatedSkuPrice);
                        }else {
                            //获取Exce中导入的价格
                            Double price1 = relatedSkuPrice.getRelatedSalesPrice();
                            Double price2 = price.getRelatedSalesPrice();
                            //如果不相等 则更新记录
                            if(!price1.equals(price2)){
                                RelatedSkuPriceRecord record=new RelatedSkuPriceRecord();
                                BeanUtils.copyProperties(record,price);
                                record.setRelatedPriceId(price.getId());
                                record.setRelatedSalesPrice(price1);
                                //更新变动记录
                                relatedSkuPriceRecordService.save(record);
                                //更新主表
                                price.setRelatedSalesPrice(price1);
                                updateByRelatedId(price);
                            }
                        }
                    }
                }
                //更新差额
                updateDifferences();
                //更新标记
                updateFlag();
            }
        }
    }

    @Override
    public Integer updateDifferences() {
        return relatedSkuPriceMapper.updateDifferences();
    }

    @Override
    public Integer updateDateByRelatedId(RelatedSkuPrice relatedSkuPrice) {
        relatedSkuPrice.setFlag(2);
        return relatedSkuPriceMapper.updateDateByRelatedId(relatedSkuPrice);
    }

    @Override
    public Integer updateFlag() {
        return relatedSkuPriceMapper.updateFlag();
    }


}
